package uit.minito.views;

import java.util.Timer;
import java.util.TimerTask;

import uit.minito.timduongxebuyt.R;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class CustomZoomControls extends LinearLayout implements OnClickListener{
	
	Button zoomInButton;
	Button zoomOutButton;
	LinearLayout zoomControlMainLayout;
	public OnCustomZoomControlsListener delegate;
	float maxAlpha=0.4f;
	int maxDuration=300;
	Timer fadeTimer=null;
	boolean isShowing;
	Activity mainActivity;
	
	private void initialize(Context context){
		LayoutInflater layoutInflater = (LayoutInflater) context
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.custom_zoom_control, this);
		mainActivity = (Activity)context;
		zoomInButton=(Button)findViewById(R.id.zoomInButton);
		zoomOutButton=(Button)findViewById(R.id.zoomOutButton);
		zoomControlMainLayout=(LinearLayout)findViewById(R.id.zoomControlMainLayout);
		//zoomInButton.setText("+");
		//zoomOutButton.setText("-");
		zoomInButton.setOnClickListener(this);
		zoomOutButton.setOnClickListener(this);
		isShowing=true;
		doFadeAnimation(1, maxAlpha,0);
		runTimer();
		isShowing=false;
	}
	
	public void runTimer (){
		if(isShowing == true){
		if(fadeTimer==null)
			fadeTimer = new Timer();
		fadeTimer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				mainActivity.runOnUiThread(new Runnable() {
					
					public void run() {
						// TODO Auto-generated method stub
						hide();
						fadeTimer.cancel();
						fadeTimer = null;
						
					}
				});
			}
			
			
		}, 2000);
		}
	}
	
	
	public void show(){
		if(isShowing == false){
			
			doFadeAnimation(0, maxAlpha,0);
		}else{
			if(fadeTimer != null){
				fadeTimer.cancel();
				fadeTimer = null;
			}
		}
		
	}
	public void hide(){
		if(isShowing == true){
			doFadeAnimation(maxAlpha, 0,maxDuration);
		}
	}
	private void doFadeAnimation(float fromAlpha,float toAlpha,int duration){
		AlphaAnimation anim=new AlphaAnimation(fromAlpha, toAlpha);
		anim.setDuration(duration);
		anim.setFillAfter(true);
		anim.setAnimationListener(new AnimationListener() {
			
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				isShowing = !isShowing;
			}
		});
		zoomControlMainLayout.startAnimation(anim);
		
	}
	public CustomZoomControls(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initialize(context);
	}

	public CustomZoomControls(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initialize(context);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.zoomInButton:
			delegate.onPressZoomingIn();
			break;

		case R.id.zoomOutButton:
			delegate.onPressZoomingOut();
			break;
		}
	}

}
